package com.mumulx.check.server;


import lombok.extern.slf4j.Slf4j;
import nxopen.NXRemotableObject;
import nxopen.Session;
import nxopen.SessionFactory;

/*
 *  Created by IntelliJ IDEA.
 *  User: 木木
 *  Date: 2020/10/29
 *  Time: 16:53
 *  用于返回nxSession的单例模式
 */
@Slf4j
public class SingleNxSession {

    private volatile static  Session theSession=null;

    // 双重检测锁模式的 懒汉式单例  DCL懒汉式
    public static Session getInstance(){
        log.info("获取NX Session实例");
        if (theSession==null){//第一层加测
            synchronized (SingleNxSession.class){//锁
                if (theSession==null){//第二层检测
                    NXRemotableObject.RemotingProtocol remotingProtocol = NXRemotableObject.RemotingProtocol.create();
                    try {
                        theSession = (Session) SessionFactory.get("Session", remotingProtocol);
                    } catch (Exception e) {
                        log.error("获取nx session失败--可能是服务器没有nx许可证，出错信息如下：");
                        e.printStackTrace();
                    }

                }
            }
        }
        return theSession;
    }
}
